package com.leyou.item.controller;

import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //查询
    @GetMapping("/of/parent")
    public List<CategoryDTO> findCategoryByParentId(@RequestParam Long pid) {
        //调用业务层获取数据结果集
        List<CategoryDTO> categoryDtoList = categoryService.findCategoryByParentId(pid);
        return categoryDtoList;
    }

    //删除
    @DeleteMapping
    public void deleteCategoryById(HttpServletResponse response, @RequestParam Long id) throws IOException {
        //调用业务层删除
        categoryService.deleteCategoryById(id);
        response.sendRedirect("/category/of/parent.html");
    }

    //根据品牌查询商品类目
    @GetMapping("/of/brand")
    public ResponseEntity<List<CategoryDTO>> queryByBrandId(@RequestParam("id") Long brandId) {
        return ResponseEntity.ok(categoryService.queryByBrandId(brandId));
    }

    //根据id的集合查询商品分类
    @GetMapping("/list")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByIds(@RequestParam("ids") List<Long> idList) {
        return ResponseEntity.ok(categoryService.queryByIds(idList));
    }

    //根据3级分类id，查询1~3级的分类
    @GetMapping("/levels")
    public ResponseEntity<List<CategoryDTO>> queryAllByCid3(@RequestParam("id") Long id) {
        return ResponseEntity.ok(categoryService.queryAllByCid3(id));
    }
}
